autocmd!
mapclear
mapclear!
noremap / <Esc>
noremap! / <Esc>
cnoremap / <C-C>
noremap  @/ /
noremap! @/ /
map [3~ 
map! [3~ 
map OP <F1>
map! OP <F1>
map OQ <F2>
map! OQ <F2>
map OR <F3>
map! OR <F3>
map OS <F4>
map! OS <F4>
map [15~ <F5>
map! [15~ <F5>
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
cnoremap ( ()<left>
cnoremap [ []<left>
cnoremap { {}<left>
set ignorecase smartcase incsearch backspace=2 linebreak hidden
set autoindent tabstop=4 history=150 mouse=a ttymouse=xterm wildmenu
set wildmode=list:longest,full display=lastline modeline t_Co=256
set listchars=tab:>\ ,eol:<
syntax on
hi NonText ctermfg=238
hi SpecialKey ctermfg=238
hi Statement ctermfg=250
hi Constant ctermfg=255
hi Identifier ctermfg=123
hi ErrorMsg ctermbg=143 ctermfg=0
hi Search ctermbg=238
au VimEnter * if !argc() | cd /sdcard-ext/vimhome | e main | en | so abbrev

set statusline=\ %n\ %{&ff}%y\ %<%F%m%=%5l/%L%4v\ 
hi clear StatusLine
hi StatusLine ctermfg=250 ctermbg=238
nnoremap <silent> K :exe 'se ls='.(&ls==2?0:2)<CR>

fun! InitFile()
	syntax region txtShadow start=/\~(/ end=/)\~/
	hi link txtShadow NonText
	syntax region txtBold start=/\t/ end=/\t/
	hi link txtBold Constant
	syntax match txtTransp /^@\|@$/
	hi txtTransp ctermfg=236
	if getline(1)=~'tw='
		nnoremap <buffer> A }b$a
		inoremap <buffer> <tab> <tab><tab><left>
	endif
endfun
au BufNewFile,BufRead *.txt call InitFile()
au BufNewFile *.txt exe 'normal! i~( vim: set nowrap ts=1 tw=72 fo=aw: )~'

let s:Menu=["'Empty String'[-1]",
\	"strftime('%l:%M',localtime()-14400)",
\	"(expand('%:h')=='.'? '':expand('%:h').'/')"
\		.".(expand('%:t')[0]=='.'? '':'.').expand('%:t').'.swp'",
\	"'/mnt/sdcard/download/'",
\	"'/data/data/org.connectbot/files/kbox/'"]
let s:MenuStr=join(map(copy(s:Menu),
\	"v:key.' '.(len(v:val)>44? v:val[:20].' .. '.v:val[-20:] : v:val)"),"\n")
fun! InsertMenu()
	echoh Constant | echo s:MenuStr | echoh None
	return eval(s:Menu[str2nr(nr2char(getchar()))])
endfun
fun! BufferMenu()
	let bufstr="\n"
	let idx=[]
	for b in range(1, bufnr('$'))
		let bufstr.=buflisted(b)? len(add(idx,b)).' '
		\	.matchstr(bufname(b),'[^/]*$')."\n" : ""
	endfor
	echoh Constant | echo bufstr | echoh None
	let sel=nr2char(getchar())
	if sel==?'x'
		exe 'redraw | bd! '.join(map(split(input(
		\	"Delete which buffers? ")),"idx[v:val-1]"))
		call BufferMenu()
	elseif str2nr(sel)
		exe 'redraw | b '.idx[sel-1]
	endif
endfun
nnoremap <F7> :call BufferMenu()<CR>
noremap! <F7> <C-R>=InsertMenu()<CR>

let s:scrollOn=0
fun! OnDrag()
	let s:inipos=winline()
	let s:initm=reltime()
endfun
fun! OnRelease()
	if !s:inipos
		return
	endif
	let vel=(winline()-s:inipos)/str2float(reltimestr(reltime(s:initm)))
	let s:inipos=0
	let linesToScroll=min([100,float2nr(vel*vel/64)])
	let wait='redraw | sleep '.min([100,float2nr(8000/vel/vel)+1]).'m'
	let scroll=vel>0 ? "normal! \<C-Y>" : "normal! \<C-E>"
	if linesToScroll > 0
		for i in range(1,linesToScroll)
			exe scroll
			exe wait
		endfor
	elseif line('.')==line('$')
		normal! zb
	endif
endfun
fun! ToggleScroll()
	let s:scrollOn = !s:scrollOn
	if s:scrollOn
		nnoremap <silent> <leftmouse> <leftmouse>:call OnDrag()<CR>
		nnoremap <silent> <leftrelease> <leftmouse>:call OnRelease()<CR>
	else
		nunmap <leftmouse>
		nunmap <leftrelease>
	endif
endfun
nnoremap @<leftmouse> :call ToggleScroll()<CR>
call ToggleScroll()

fun! CapWait(prev)
	redraw
	let input = nr2char(getchar())
	if input=~'[.?!\r\n[:blank:]]'
		exe 'normal! i' . input . "\<Right>"
		return CapWait(input)
	else
		if input=~'[\e\/]' 
			return "\<del>"
		elseif a:prev=~'[\r\n[:blank:]]'
			return toupper(input) . "\<del>"
		else 
			return input . "\<del>"
		endif
	endif
endfun
fun! CapHere()
	let trunc = getline(".")[0:col(".")-2] 
	return col(".")==1 ? CapWait("\r")
	\	: trunc=~'[?!.]\s*$\|^\s*$' ? CapWait(trunc[-1:-1])
	\	: "\<del>"
endfun
imap <silent> . ._<Left><C-R>=CapWait('.'<CR>
imap <silent> ? ?_<Left><C-R>=CapWait('?'<CR>
imap <silent> ! !_<Left><C-R>=CapWait('!'<CR>
imap <silent> <CR> <CR>_<Left><C-R>=CapWait("\r"<R>
imap <silent> <NL> <NL>_<Left><C-R>=CapWait("\n"<CR>
nmap <silent> O O_<Left><C-R>=CapWait("\r"<CR>
nmap <silent> o o_<Left><C-R>=CapWait("\r"<CR>
nmap <silent> a a_<Left><C-R>=CapHere(<CR>
nmap <silent> A $a_<Left><C-R>=CapHere(<CR>
nmap <silent> i i_<Left><C-R>=CapHere(<CR>
nmap <silent> I I_<Left><C-R>=CapHere(<CR>
